package tungsten;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import tungsten.pb.PBEncoder;
import tungsten.pb.PBMessage;
import tungsten.pb.protocol.LoginProto;

/**
 * Created by Lv on 2016/6/15.
 */
public class Client {

    public static void main(String[] args) throws Exception
    {
        new Client().connect(8090);
    }

    private class Decoder extends ChannelInboundHandlerAdapter
    {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
        {
            ByteBuf buf = (ByteBuf) msg;

            final int mId = buf.readInt();

            final byte[] array;
            final int offset;
            final int length = buf.readableBytes();


        }
    }

    public void connect(int port) throws Exception
    {
        EventLoopGroup group = new NioEventLoopGroup();

        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY,true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
                        ch.pipeline().addLast(new Decoder());

                        ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
                        ch.pipeline().addLast(new PBEncoder());
                        ch.pipeline().addLast(new SubReqClientHandler());
                    }
                });
        ChannelFuture f = b.connect("127.0.0.1",port).sync();

        f.addListener(new GenericFutureListener(){
            @Override
            public void operationComplete(Future future) throws Exception {
                System.out.println("AAAAAAAAAAAA");
            }
        });
        f.channel().closeFuture().sync();


    }

    private class SubReqClientHandler extends ChannelInboundHandlerAdapter
    {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("Receive server response:[" + msg + "]");
        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception
        {
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
                    LoginProto.LoginReq.Builder builder = LoginProto.LoginReq.newBuilder();
                    builder.setUsename("Lv");
                    builder.setPassword("111111");

                    PBMessage message = new PBMessage(LoginProto.ModuleType.TYPE_FIELD_NUMBER,LoginProto.Req.newBuilder().setLogin(builder).build());

                    ctx.write(message);
                    ctx.flush();
//                }
//            }).start();

        }
    }
}
